﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ThridGroup.ERP.DTO.WarehouseManagement;
using Volo.Abp.Application.Services;
using Volo.Abp.Domain.Repositories;

namespace ThridGroup.ERP.WarehouseManagement
{
    /// <summary>
    /// 装箱Services
    /// </summary>
    public class PackingServices : ApplicationService, IPackingServices
    {
        /// <summary>
        /// 装箱仓储
        /// </summary>
        private readonly IRepository<PackingModelS, int> _packingServices;
        /// <summary>
        /// 装箱明细
        /// </summary>
        private readonly IRepository<PackingDetailsModelS, int> _packingDetailsModelSServices;

        /// <summary>
        /// 构造方法
        /// </summary>
        /// <param name="packingServices">装箱仓储</param>
        /// <param name="_packingDetailsModelSServices">装箱明细</param>
        public PackingServices(IRepository<PackingModelS, int> packingServices, IRepository<PackingDetailsModelS, int> packingDetailsModelSServices)
        {
            _packingServices = packingServices;
            _packingDetailsModelSServices = packingDetailsModelSServices;
        }

        /// <summary>
        /// 添加装箱
        /// </summary>
        /// <param name="dto">装箱信息</param>
        /// <returns></returns>
        public async Task<int> CreatePacking(QueryPackingDto dto)
        {
            var entity = ObjectMapper.Map<QueryPackingDto, PackingModelS>(dto);
            var result = await _packingServices.InsertAsync(entity);
            return result == null ? 0 : 1;
        }

        /// <summary>
        /// 查询装箱（分页/条件）
        /// </summary>
        /// <param name="dto">查询条件</param>
        /// <returns></returns>
        public async Task<PackingQueryResult> QueryPacking(GetPackingDto dto)
        {
            var packingList = await _packingServices.GetListAsync();
            
            // 装箱单编号查询
            packingList = packingList.WhereIf(!string.IsNullOrEmpty(dto.PackingCode), t => t.PackingCode.Contains(dto.PackingCode)).ToList();
            
            // 销售订单编号查询
            packingList = packingList.WhereIf(!string.IsNullOrEmpty(dto.SaleOrderCode), t => t.SaleOrderCode.Contains(dto.SaleOrderCode)).ToList();
            
            // 客户名称查询
            packingList = packingList.WhereIf(!string.IsNullOrEmpty(dto.CustomerName), t => t.CustomerName.Contains(dto.CustomerName)).ToList();
            
            // 检查员查询
            packingList = packingList.WhereIf(!string.IsNullOrEmpty(dto.Inspector), t => t.Inspector.Contains(dto.Inspector)).ToList();

            // 获取总记录数
            var totalCount = packingList.Count;
            
            // 分页处理
            var pagedList = packingList.Skip((dto.page - 1) * dto.limit).Take(dto.limit).ToList();
            var mappedList = ObjectMapper.Map<List<PackingModelS>, List<QueryPackingDto>>(pagedList);

            return new PackingQueryResult
            {
                Data = mappedList,
                TotalCount = totalCount
            };
        }

        /// <summary>
        /// 修改装箱
        /// </summary>
        /// <param name="dto">装箱信息</param>
        /// <returns></returns>
        public async Task<int> UpdatePacking(QueryPackingDto dto)
        {
            var entity = ObjectMapper.Map<QueryPackingDto, PackingModelS>(dto);
            var result = await _packingServices.UpdateAsync(entity);
            return result == null ? 0 : 1;
        }

        /// <summary>
        /// 批量删除装箱
        /// </summary>
        /// <param name="ids">装箱ID列表</param>
        /// <returns></returns>
        public async Task BatchDeletePacking(List<int> ids)
        {
            if (ids == null || !ids.Any())
                return;
            await _packingServices.DeleteDirectAsync(t => ids.Contains(t.Id));
        }

        /// <summary>
        /// 根据ID获取装箱信息
        /// </summary>
        /// <param name="id">装箱ID</param>
        /// <returns></returns>
        public async Task<QueryPackingDto> GetPackingById(int id)
        {
            var entity = await _packingServices.GetAsync(id);
            return ObjectMapper.Map<PackingModelS, QueryPackingDto>(entity);
        }

        /// <summary>
        /// 根据ID获取装箱信息修改状态
        /// </summary>
        /// <param name="id">装箱ID</param>
        /// <returns></returns>
        public async Task<int> GetPackingStatus(int id)
        {
            var entity = await _packingServices.GetAsync(id);
            entity.State = "1"; // 假设状态改为"1"表示已完成
            var result = await _packingServices.UpdateAsync(entity);
            return result == null ? 0 : 1;
        }

        /// <summary>
        /// 添加装箱明细
        /// </summary>
        /// <returns></returns>
        public async Task<int> AddPackingDetailsModelS(PackingDetailsModelDto packingDetailsModelDto) 
        {
            var entity = ObjectMapper.Map<PackingDetailsModelDto, PackingDetailsModelS>(packingDetailsModelDto);
            var result = await _packingDetailsModelSServices.InsertAsync(entity);
            return result == null ? 0 : 1;
        }
        /// <summary>
        ///显示装箱明细
        /// </summary>
        /// <param name="page"></param>
        /// <param name="limit"></param>
        /// <returns></returns>
        public async Task<List<PackingDetailsModelDto>> QueryPackingDetailsModel(string PackingCode, int page = 1,int limit=3) 
        {
            var detailsList = await _packingDetailsModelSServices.GetListAsync();
            
                detailsList = detailsList.Where(d => d.PackingCode == PackingCode).ToList();
            
            var pagedList = detailsList.Skip((page - 1) * limit).Take(limit).ToList();
            var mappedList = ObjectMapper.Map<List<PackingDetailsModelS>, List<PackingDetailsModelDto>>(pagedList);
            return mappedList;
        }

        /// <summary>
        /// 修改装箱明细
        /// </summary>
        /// <param name="dto">装箱明细信息</param>
        /// <returns></returns>
        public async Task<int> UpdatePackingDetailsModel(PackingDetailsModelDto dto)
        {
            var entity = ObjectMapper.Map<PackingDetailsModelDto, PackingDetailsModelS>(dto);
            var result = await _packingDetailsModelSServices.UpdateAsync(entity);
            return result == null ? 0 : 1;
        }

        /// <summary>
        /// 批量删除装箱明细
        /// </summary>
        /// <param name="ids">装箱明细ID列表</param>
        /// <returns></returns>
        public async Task<int> BatchDeletePackingDetailsModel(List<int> ids)
        {
            if (ids == null || !ids.Any())
                return 0;
            await _packingDetailsModelSServices.DeleteDirectAsync(t => ids.Contains(t.Id));
            return 1;
        }
    }
}
